package com.cll.learn.rpc.consumer;

import com.cll.learn.rpc.api.entity.User;
import com.cll.learn.rpc.api.service.UserService;
import com.cll.learn.rpc.consumer.context.RpcConsumer;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 描述信息:
 * 消费端启动引导类
 * @author CLL
 * @version 1.0
 * @date 2020/9/14 15:43
 */
public class ConsumerBootstrap {

    private static final Logger logger = LoggerFactory.getLogger(ConsumerBootstrap.class);

    public static void main(String[] args) throws InterruptedException {
        if (null == args || 2 != args.length) {
            return;
        }
        int defaultServerPort = 8888;
        if (NumberUtils.isDigits(args[0])) {
            defaultServerPort = Integer.parseInt(args[0]);
        }
        String username = args[1];
        RpcConsumer rpcConsumer = new RpcConsumer("127.0.0.1", defaultServerPort);
        UserService proxy = rpcConsumer.createProxy(UserService.class);

        for (int i = 0; i < 20; i++) {
            int split = i % 2;
            List<User> userList = proxy.select(new User(split, username + split, "cll", new Date()));
            logger.info("result = {}", userList);
            for (User user : userList) {
                logger.info("user = {}", user);
            }
            Thread.sleep(3000);
        }
    }
}